package com.example.myapplication.tack_14;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.tack_14.adapter.ArrangeAdapter;
import com.example.myapplication.tack_14.data.ArrangeData;
import com.example.myapplication.tack_14.data.BannerData;
import com.example.myapplication.tack_14.data.FenLeiData;
import com.example.myapplication.utils.BannerTool;
import com.example.myapplication.utils.OkHttpTool;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class WasteActivity extends AppCompatActivity {
    private ImageView exit;
    private ViewPager banner;
    private TextView classification;
    private TextView search;
    private TabLayout fnelei;
    private RecyclerView arrange;
    private int id =7;
    private List<FenLeiData.RowsDTO> listFenLei;
    private List<ArrangeData.RowsDTO> listArrangeData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_14_waste_activity);
        init();
        exit_();
        getBanner();
        fenLei();
        cilak();
        getArrange(id);
    }

    private void getArrange(int id) {
        String url = "prod-api/api/garbage-classification/news/list?type="+id+"&pageNum=1&pageS";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ArrangeData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                ArrangeData arrangeData = (ArrangeData)obj;
                if(arrangeData.getCode() == 200){
                    listArrangeData = arrangeData.getRows();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrangeAdapter();
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void ArrangeAdapter() {
        ArrangeAdapter arrangeAdapter = new ArrangeAdapter(listArrangeData, WasteActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(WasteActivity.this);
        arrange.setAdapter(arrangeAdapter);
        arrange.setLayoutManager(linearLayoutManager);
    }

    private void fenLei() {
        String url = "prod-api/api/garbage-classification/news-type/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, FenLeiData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                FenLeiData fenLeiData = (FenLeiData)obj;
                if(fenLeiData.getCode() == 200){
                    listFenLei = fenLeiData.getRows();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            for (FenLeiData.RowsDTO s : listFenLei) {
                                TabLayout.Tab tab = fnelei.newTab();
                                tab.setText(s.getName());
                                fnelei.addTab(tab);
                            }
                            Sort();
                        }
                    });
                }


            }

            @Override
            public void onFailure(String msg) {

            }
        });



    }

    private void Sort() {
        fnelei.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                id = listFenLei.get(position).getId();
                getArrange(id);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void cilak() {
        classification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WasteActivity.this,ClassificationActivity.class);
                WasteActivity.this.startActivity(intent);
            }
        });
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WasteActivity.this,SearchActivity.class);
                WasteActivity.this.startActivity(intent);
            }
        });
    }

    private void getBanner() {
        String url = "prod-api/api/garbage-classification/ad-banner/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, BannerData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                BannerData bannerData = (BannerData)obj;
                if(bannerData.getCode() == 200){

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayList<String> arrayList = new ArrayList<>();
                            for (BannerData.DataDTO row : bannerData.getData()) {
                                arrayList.add(row.getImgUrl());
                            }
                            BannerTool bannerTool = new BannerTool(WasteActivity.this,arrayList,banner);
                            banner.setAdapter(bannerTool);
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WasteActivity.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        banner = findViewById(R.id.banner);
        classification = findViewById(R.id.classification);
        search = findViewById(R.id.search);
        arrange = findViewById(R.id.arrange);
        fnelei = findViewById(R.id.fnelei);

    }
}